package person.learn;

import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import person.learn.interceptors.URLInterceptor;
import person.learn.jacksonmessageconverter.JSONFormatMethodProcessor;
import person.learn.messageconverter.MyMessageConverter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by barton on 16-1-25.
 * 1.@EnableWebMvc
 */
@SpringBootApplication
@RestController
//@EnableAsync
public class SpringBootLearnApplication extends WebMvcConfigurerAdapter {

    public static void main(String[] args) {
        // args : 传递给SpringApplication的构造器参数是spring
        // beans的配置源。在大多数情况下，这些将是@Configuration类的引用，但它们也可能是XML配置或要扫描包的引用
        SpringApplication.run(SpringBootLearnApplication.class, args);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new URLInterceptor()).addPathPatterns("/interceptor/beilanjie");
    }

    @RequestMapping("/hello")
    public JSONObject hello() {
        JSONObject object = new JSONObject();
        object.put("name", "springboot");
        object.put("age", "26");
        object.put("description", "Hello World! Hello Spring-Boot!");
        return object;
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/converter").setViewName("/converter");
        registry.addViewController("/sse").setViewName("/sse");
        registry.addViewController("/ws").setViewName("/thymeleaf/ws");
        registry.addViewController("/chat").setViewName("/thymeleaf/chat");
        registry.addViewController("/login_ws").setViewName("/thymeleaf/login_ws");
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        returnValueHandlers.add(new JSONFormatMethodProcessor(messageConverter()));
    }

    private List<HttpMessageConverter<?>> messageConverter() {
        List<HttpMessageConverter<?>> converters = new ArrayList<>();
        converters.add(new ByteArrayHttpMessageConverter());
        converters.add(new StringHttpMessageConverter());
        converters.add(new ResourceHttpMessageConverter());
        converters.add(new SourceHttpMessageConverter<>());
        converters.add(new AllEncompassingFormHttpMessageConverter());
        converters.add(new Jaxb2RootElementHttpMessageConverter());
        converters.add(new MappingJackson2HttpMessageConverter());
        return converters;
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());
    }

    @Bean
    public MyMessageConverter converter() {
        return new MyMessageConverter();
    }

//    @Bean
//    public JsonFormatHttpMessageConverter jsonFormatHttpMessageConverter() {
//        return new JsonFormatHttpMessageConverter();
//    }
}
